package com.my.service.impl;

import com.my.service.AddressService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器

    private String getUrl(String str) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("address-provider");
        return "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/address/" + str;
    }

    @Override
    public String showAddress(HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("show"), request, String.class);
        return response.getBody();
    }

    @Override
    public String addAddress(String province, String city, String district, String address, String code, String phone, String name, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("province", province);
        map.add("city", city);
        map.add("district", district);
        map.add("address", address);
        map.add("code", code);
        map.add("phone", phone);
        map.add("name", name);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("add"), request, String.class);
        return response.getBody();
    }

    @Override
    public String updateAddress(Integer id, String province, String city, String district, String address, String code, String phone, String name) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        map.add("province", province);
        map.add("city", city);
        map.add("district", district);
        map.add("address", address);
        map.add("code", code);
        map.add("phone", phone);
        map.add("name", name);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("update"), request, String.class);
        return response.getBody();
    }

    @Override
    public String deleteAddress(Integer addressId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("addressId", addressId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("delete"), request, String.class);
        return response.getBody();
    }

    @Override
    public String selById(Integer id) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("selById"), request, String.class);
        return response.getBody();
    }

    @Override
    public String updateStatus(Integer ori_id, Integer id) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
        map.add("ori_id", ori_id);
        map.add("id", id);
        HttpEntity<MultiValueMap<String, Integer>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("updateStatus"), request, String.class);
        return response.getBody();
    }
}
